package com.example.mediamoudle.crop

import android.annotation.SuppressLint
import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.MotionEvent
import androidx.recyclerview.widget.RecyclerView
import com.example.mediamoudle.R
import com.google.android.material.bottomsheet.BottomSheetDialog

/**
 * @author: Mr.You
 * @create: 2022-11-29 14:21
 * @description:
 **/
class VideoBottomDialog(context: Context):BottomSheetDialog(context) {

    private lateinit var mRecycleView:RecyclerView
    private lateinit var mThumbnailProgressView: ThumbnailProgressView
    private lateinit var thumbAdapter:ThumbAdapter
    var isScroll = false
    private var duration = 0
    @SuppressLint("ClickableViewAccessibility")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.layout_bottom_video)
        mRecycleView = findViewById<RecyclerView>(R.id.mRecyclerView_Dialog)!!
        mThumbnailProgressView = findViewById<ThumbnailProgressView>(R.id.thumbProgressView_Dialog)!!
        mRecycleView.addItemDecoration(RecyclerDivider(context.resources.getColor(com.android.baselibrary.R.color.color_808080)))
        thumbAdapter = ThumbAdapter()
        mRecycleView.adapter = thumbAdapter
        mRecycleView.addOnScrollListener(object :RecyclerView.OnScrollListener(){
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)

                if (isScroll){
                    var extent = recyclerView.computeHorizontalScrollExtent();
                    Log.i("TAGasd", "extent: $extent")
                    //整体的高度，注意是整体，包括在显示区域之外的
                    var range = recyclerView.computeHorizontalScrollRange();
                    Log.i("TAGasd", "range: $range")
                    //已经向下滚动的距离，为0时表示已处于顶部
                    var offset = recyclerView.computeHorizontalScrollOffset();
                    Log.i("TAGasd", "offset: $offset")
                    //已经滚动的百分比 [0.0-1.0]
                    var percentage = (offset / (range - extent).toFloat())
                    var currentTime = duration * percentage
                  // *//* if (mBinding.videoPreview.isPlaying) mBinding.videoPreview.pause()
                    //mBinding.videoPreview.seekTo(currentTime.toInt())*//*
                }

            }

            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
            }

        })

        mRecycleView.setOnTouchListener { v, event ->
            when(event.action){
                MotionEvent.ACTION_DOWN ->{

                    //1.按下记录坐标
                    isScroll = true
                }
                MotionEvent.ACTION_UP ->{
                    isScroll = false
                }

            }

            return@setOnTouchListener false
        }


    }

    }



